//
//  SetInforsViewController.m
//  ZFJFormKit
//
//  Created by 张福杰 on 2018/4/24.
//  Copyright © 2019 张福杰. All rights reserved.
//

#import "SetInforsViewController.h"

#import "ZFJFormKit.h"

//自定义CELL
#import "SaveCell.h"

//下面与项目无关的
#import "MCPickerView.h"
#import "MCPickerModel.h"
#import "MBProgressHUD+MBP.h"

@interface SetInforsViewController ()<MCPickerViewDelegate>

@property (nonatomic,strong) ZFJFormTableView *tableView;
@property (nonatomic,strong) NSMutableArray<ZFJFormModel *> *dataArray;

//下面与项目无关的
@property (nonatomic,strong) MCPickerView *picker;
@property (nonatomic , strong)  __block NSMutableArray *province;
@property (nonatomic , strong)  __block NSMutableArray *city;
@property (nonatomic , strong)  __block NSMutableArray *town;

@end

@implementation SetInforsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setBaseBackButtonView];
    [self uiConfig];
    [self dataConfig];
}

- (void)uiConfig{
    WEAKBLOCK;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"个人信息";
    
    [self.view addSubview:self.tableView];
    
    self.tableView.didSelectRowBlock = ^(NSIndexPath * _Nullable indexPath, ZFJFormModel * _Nullable model) {
        [weakSelf.view endEditing:YES];
        NSLog(@"model = %@", model);
        if(indexPath.row == 3){
            //性别
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"性别" message:@"请选择性别" preferredStyle:UIAlertControllerStyleActionSheet];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }]];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                model.value = @"男";
                [weakSelf.dataArray replaceObjectAtIndex:indexPath.row withObject:model];
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }]];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                model.value = @"女";
                [weakSelf.dataArray replaceObjectAtIndex:indexPath.row withObject:model];
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }]];
            
            [alertController addAction:[UIAlertAction actionWithTitle:@"保密" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                model.value = @"保密";
                [weakSelf.dataArray replaceObjectAtIndex:indexPath.row withObject:model];
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }]];
            
            [weakSelf presentViewController:alertController animated:YES completion:nil];
        }else if(indexPath.row == 6){
            //地址
            [weakSelf showPicker];
        }
    };
}

- (void)dataConfig{
    
    //CELL的通用配置Model，也可以根据不同的CELL分别配置
    ZFJFormCellConfig *configModel = [[ZFJFormCellConfig alloc] init];
    //左边title
    configModel.titleColor = [UIColor blackColor];
    configModel.titleFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    //头像圆角尺寸
    configModel.headImgRadius = 5;
    //CELL右边值得颜色和字体
    configModel.valueColor = [UIColor blueColor];
    configModel.valueFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    //分割线的配置
    configModel.separatorLineColor = [UIColor groupTableViewBackgroundColor];
    configModel.isHiddenLine = NO;
    //占位符颜色
    configModel.placeholderColor = [UIColor colorWithRed:0.776 green:0.776 blue:0.800 alpha:1.00];
    //控件左右两边的间距
    configModel.marginSize = 15;
    
    //头像
    ZFJFormModel *headImg_model = [[ZFJFormModel alloc] init];
    //CELL类型
    headImg_model.formCellType = KFormCellHeadImgType;
    //CELL的通用配置Model，也可以根据不同的CELL分别配置
    headImg_model.configModel = configModel;
    headImg_model.title = @"头像";
    headImg_model.iconObject = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561278644354&di=3cc92ef55c2336b29b1fe09cbf614705&imgtype=0&src=http%3A%2F%2Fimg4q.duitang.com%2Fuploads%2Fitem%2F201408%2F08%2F20140808171354_XkhfE.jpeg";
    headImg_model.height = 100;
    headImg_model.isCanSelect = YES;
    headImg_model.isShowCellRightImg = YES;
    [self.dataArray addObject:headImg_model];
    
    //姓名 不能修改，所以不能输入
    ZFJFormModel *name_model = [[ZFJFormModel alloc] init];
    name_model.formCellType = KFormCellLabelType;
    name_model.configModel = configModel;
    name_model.title = @"姓名";
    name_model.value = @"张福杰";
    name_model.height = 50;
    [self.dataArray addObject:name_model];
    
    //昵称（单行输入 KFormCellTextFieldType）
    ZFJFormModel *nickName_model = [[ZFJFormModel alloc] init];
    nickName_model.formCellType = KFormCellTextFieldType;
    nickName_model.configModel = configModel;
    nickName_model.title = @"昵称";
    nickName_model.placeholder = @"请输入您的昵称";
    nickName_model.height = 50;
    nickName_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
        if(model.value <= 0){
            [MBProgressHUD SHOWPrompttext:model.placeholder];
            return NO;
        }
        return YES;
    };
    [self.dataArray addObject:nickName_model];
    
    //性别（不能输入 KFormCellLabelType）
    ZFJFormModel *sex_model = [[ZFJFormModel alloc] init];
    sex_model.formCellType = KFormCellLabelType;
    sex_model.configModel = configModel;
    sex_model.title = @"性别";
    sex_model.placeholder = @"请选择性别";
    sex_model.height = 50;
    sex_model.isCanSelect = YES;
    sex_model.isShowCellRightImg = YES;
    sex_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
        if(model.value <= 0){
            [MBProgressHUD SHOWPrompttext:model.placeholder];
            return NO;
        }
        return YES;
    };
    [self.dataArray addObject:sex_model];
    
    //电话号码
    ZFJFormModel *phone_model = [[ZFJFormModel alloc] init];
    phone_model.formCellType = KFormCellTextFieldType;
    //设置键盘类型（这里设置数字键盘）
    phone_model.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    phone_model.configModel = configModel;
    phone_model.title = @"电话";
    phone_model.placeholder = @"请输入您的电话号码";
    phone_model.height = 50;
    phone_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
        if(model.value <= 0){
            [MBProgressHUD SHOWPrompttext:model.placeholder];
            return NO;
        }
        return YES;
    };
    [self.dataArray addObject:phone_model];
    
    //选择器（KFormCellSwitchType）
    ZFJFormModel *switch_model = [[ZFJFormModel alloc] init];
    switch_model.formCellType = KFormCellSwitchType;
    switch_model.configModel = configModel;
    switch_model.title = @"是否开启好友推荐";
    switch_model.placeholder = @"请选择";
    switch_model.height = 50;
    switch_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
        if(model.value == nil){
            [MBProgressHUD SHOWPrompttext:model.placeholder];
            return NO;
        }
        return YES;
    };
    [self.dataArray addObject:switch_model];
    
    //地址（不能输入 KFormCellLabelType）
    ZFJFormModel *birthday_model = [[ZFJFormModel alloc] init];
    birthday_model.formCellType = KFormCellLabelType;
    birthday_model.configModel = configModel;
    birthday_model.title = @"地址";
    birthday_model.placeholder = @"请选择您的地址";
    birthday_model.height = 50;
    birthday_model.isCanSelect = YES;
    birthday_model.isShowCellRightImg = YES;
    birthday_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
        if(model.value <= 0){
            [MBProgressHUD SHOWPrompttext:model.placeholder];
            return NO;
        }
        return YES;
    };
    [self.dataArray addObject:birthday_model];
    
    //个人简介（KFormCellTextViewType 多行输入）
    ZFJFormModel *introduction_model = [[ZFJFormModel alloc] init];
    introduction_model.formCellType = KFormCellTextViewType;
    introduction_model.configModel = configModel;
    introduction_model.title = @"个人简介";
    introduction_model.placeholder = @"请输入您的个人简介";
    introduction_model.height = 50;
    introduction_model.textView_maxHeight = 100;
    introduction_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
        if(model.value <= 0){
            [MBProgressHUD SHOWPrompttext:model.placeholder];
            return NO;
        }
        return YES;
    };
    [self.dataArray addObject:introduction_model];
    
    //自定义CELL（KFormCellCustomType 保存）
    ZFJFormModel *custom_model = [[ZFJFormModel alloc] init];
    custom_model.formCellType = KFormCellCustomType;
    custom_model.configModel = configModel;
    custom_model.customCls = [SaveCell class];
    custom_model.height = 120;
    custom_model.isCanSelect = YES;
    //自定义CELL事件处理
    custom_model.customCellEventBlock = ^(id  _Nonnull obj) {
        NSLog(@"obj == %@",obj);
        [ZFJFormTool validateDataArray:self.dataArray];
    };
    [self.dataArray addObject:custom_model];
    
    self.tableView.dataArray = self.dataArray;
}

- (ZFJFormTableView *)tableView{
    if (_tableView == nil){
        ZFJFormConfig *formConfig = [[ZFJFormConfig alloc] init];
        formConfig.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        _tableView = [[ZFJFormTableView alloc] initWithFrame:CGRectMake(0, KNavBarHei, ZFJForm_ScreenWidth, ZFJForm_ScreenHeight - KNavBarHei) config:formConfig];
    }
    return _tableView;
}

- (NSMutableArray<ZFJFormModel *> *)dataArray{
    if (_dataArray == nil){
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

#pragma mark - 无用的不需要看的 -
- (void)showPicker{
    [self.view addSubview:self.picker];
}

- (NSMutableArray *)province{
    if(_province == nil){
        _province = [NSMutableArray array];
        for (int i = 0; i<5; i++) {
            MCPickerModel *model = [[MCPickerModel alloc]init];
            model.name = [NSString stringWithFormat:@"安徽省%d",i];
            model.pid = @"123";
            [_province addObject:model];
        }
    }
    return _province;
}

- (MCPickerView *)picker{
    if(_picker == nil){
        _picker = [[MCPickerView alloc]initWithFrame:self.view.bounds];
        _picker.dataArray = self.province;
        _picker.delegate = self;
        _picker.totalLevel = 3;//共有3层数据
        _picker.titleText = @"选择地区";
    }
    return _picker;
}

- (void)MCPickerView:(MCPickerView *)MCPickerView complete:(NSString *)complete{
    NSLog(@"===%@",complete);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
    ZFJFormModel *model = [self.dataArray objectAtIndex:6];
    model.value = complete;
    [self.dataArray replaceObjectAtIndex:indexPath.row withObject:model];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)MCPickerView:(MCPickerView *)MCPickerView didSelcetedRow:(NSInteger)Row value:(MCPickerModel *)value{
    if (Row == 0) {
        __block NSMutableArray *models = [NSMutableArray array];
        for (int i=0; i<5; i++) {
            MCPickerModel *model = [[MCPickerModel alloc]init];
            model.name = [NSString stringWithFormat:@"合肥市%d",i];
            model.pid = @"123";
            [models addObject:model];
        }
        _picker.dataArray = models;
    }else if(Row == 1){
        __block NSMutableArray *models = [NSMutableArray array];
        for (int i=0; i<5; i++) {
            MCPickerModel *model = [[MCPickerModel alloc]init];
            model.name = [NSString stringWithFormat:@"蜀山区%d",i];
            model.pid = @"123";
            [models addObject:model];
        }
        _picker.dataArray = models;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
